<template>
  <view class="order-container">
    <!-- 顶部导航 -->
    <nav-bar></nav-bar>
    <view class="content-cont">
      <!-- 地址 -->
      <view class="address-cont">
        <view class="add-address" v-if="!selectAddress" @click="toAddress">
          请添加地址
          <text class="iconfont icon-arrow-right"></text>
        </view>
        <view class="select-address" v-else @click="toAddress">
          <view class="info">
            <view class="user">
              <text class="name">{{ selectAddress.name }}</text>
              <text class="phone">{{ selectAddress.phone }}</text>
            </view>
            <view class="address">
              <text class="city">{{ selectAddress.city }}</text>
              <text class="region">{{ selectAddress.region }}</text>
              <text class="detail">{{ selectAddress.detailAddress }}</text>
            </view>
          </view>
          <text class="iconfont icon-arrow-right"></text>
        </view>
      </view>
      <!-- 配送时间 -->
      <view class="time-cont">
        <view>
          <text class="iconfont icon-shizhong"></text>
          配送时间
        </view>
        <view class="address-tips" v-if="!selectAddress">请先选择配送地址</view>
      </view>
      <!-- 商品列表 -->
      <view class="goods-list">
        <view class="goods-item" v-for="goods in cartList" :key="goods.id">
          <view class="left">
            <image :src="goods.img" mode="" />
          </view>
          <view class="info">
            <view class="ch-name">{{ goods.name }}</view>
            <view class="price">￥{{ goods.selectItem.price }}</view>
            <view class="tips" v-show="goods.selectItem.fittings">
              标配 <text>含{{ goods.selectItem.fittings }}</text>
            </view>
          </view>
          <view class="right">
            <view class="weight">
              {{ goods.selectItem.spec }}({{ goods.selectItem.weight }})
              <text class="num">x{{ goods.num }}</text>
            </view>
          </view>
        </view>
      </view>
      <!-- 选择项 -->
      <view class="other-cont">
        <view class="other-item">
          <view class="left">
            <text class="iconfont icon-llingquxianjinquan"></text>
            现金卡/储值卡/小食卡
          </view>
          <view class="right">无可用</view>
        </view>
        <view class="other-item">
          <view class="left">
            <text class="iconfont icon-yue"></text>
            余额
          </view>
          <view class="right">无可用</view>
        </view>
        <view class="other-item">
          <view class="left">
            <text class="iconfont icon-jifen"></text>
            积分
          </view>
          <view class="right">无可用</view>
        </view>
      </view>
      <!-- 底部支付 -->
      <view class="footer-cont">
        <view class="footer">
          <view class="price">
            ￥<text>{{ totalPrice.toFixed(2) }}</text>
          </view>
          <view class="payment-btn" @click="subOrder">提交订单</view>
        </view>
      </view>
      <!-- 收款模态款 -->
      <u-overlay :show="isShowModel" @click="cancel">
        <view class="cont">
          <view class="title">感谢您的支持</view>
          <view class="tips">1毛钱即可</view>
          <image src="../../static/images/wx-payment.jpg" mode="" />
        </view>
      </u-overlay>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapState } from 'vuex'
export default {
  data() {
    return {
      isShowModel: false,
    }
  },
  computed: {
    ...mapGetters('cart', ['totalPrice']),
    ...mapState('address', ['addressList']),
    cartList() {
      return this.$store.state.cart.cartList.filter(item => item.isCheck)
    },
    // 选中的收货地址
    selectAddress() {
      const { addressList } = this

      /**
       * 选中的优先级高于默认地址
       * 选中和默认都是唯一的
       */

      // 没有收货地址
      if (!addressList.length) return
      const isCheck = addressList.some(item => item.isCheck === true)
      // 没有选中的地址
      if (!isCheck) {
        const isDefault = addressList.some(item => item.isDefault === true)
        // 没有默认地址
        if (!isDefault) return
        // 有默认地址
        return addressList.filter(item => item.isDefault === true)[0]
      } else {
        // 有选中地址
        return addressList.filter(item => item.isCheck === true)[0]
      }
    },
  },
  methods: {
    // 跳转选择地址页
    toAddress() {
      uni.navigateTo({
        url: '/pages/address/address',
      })
    },
    // 提交订单回调
    subOrder() {
      if (!this.selectAddress) {
        return uni.showToast({
          title: '请先选择收货地址',
          duration: 2000,
          icon: 'none',
        })
      }
      this.isShowModel = true
    },
    cancel() {
      this.isShowModel = false
      uni.showToast({
        title: '先试试其他功能吧',
        duration: 2000,
        icon: 'none',
      })
    },
  },
  onShow() {
    this.$store.dispatch('address/GetAddressList')
  },
}
</script>

<style lang="scss">
.order-container {
  padding: 0 20rpx;
  .select-address {
    display: flex;
    justify-content: space-between;
    height: 100rpx;
    border-bottom: 1px solid #eee;
    .info {
      width: 620rpx;
      margin-left: 12rpx;
      .user {
        margin-bottom: 8rpx;
        .name {
          margin-right: 12rpx;
        }
      }
      .address {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
    .iconfont {
      height: 100%;
      flex: 1;
      text-align: center;
      line-height: 100rpx;
      font-size: $lxins-fs-22;
    }
  }
  .add-address,
  .time-cont {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20rpx 0;
    color: #999;
    border-bottom: 1px solid #eee;
    .iconfont {
      font-size: $lxins-fs-18;
    }
  }

  .time-cont {
    color: #333;
    font-size: $lxins-fs-14;

    .iconfont {
      vertical-align: bottom;
      color: #999;
      font-weight: normal;
      margin-right: 12rpx;
    }
    .address-tips {
      font-weight: 600;
    }
  }
  // 商品列表
  .goods-item {
    padding: 12rpx 20rpx;
    height: 200rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border: 1px solid #eee;
    margin: 12rpx 0;
    & > view {
      height: 100%;
    }
    .left {
      display: flex;
      align-items: center;
      image {
        width: 180rpx;
        height: 180rpx;
      }
    }
    .info {
      margin-left: 20rpx;
      margin-top: 20rpx;
      width: 300rpx;
      font-size: $lxins-fs-14;
      & > view {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .ch-name {
        font-weight: 600;
      }
      .price {
        font-size: $lxins-fs-12;
        line-height: 50rpx;
      }
      .tips {
        font-weight: 600;
        font-size: $lxins-fs-12;
        text {
          margin-left: 8rpx;
          font-weight: normal;
          color: #999;
        }
      }
    }
    .right {
      display: flex;
      align-items: center;
      .weight {
        font-size: $lxins-fs-12;
        color: #999;
        text {
          margin-left: 12rpx;
          color: #333;
          font-size: 28rpx;
        }
      }
    }
  }
  // 其他选择项
  .other-cont {
    .other-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20rpx 0;
      border-bottom: 1px solid #eee;
      font-size: $lxins-fs-14;
      .iconfont {
        margin-right: 12rpx;
      }
    }
  }
  // 底部支付
  .footer-cont {
    height: 100rpx;
    .footer {
      position: fixed;
      bottom: 0;
      left: 0;
      right: 0;
      height: 100rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-top: 1px solid #eee;
      line-height: 80rpx;
      .price {
        margin-left: 20rpx;
        color: red;
        text {
          font-size: $lxins-fs-22;
        }
      }
      .payment-btn {
        background-color: $lxins-color-yellow;
        width: 300rpx;
        height: 100%;
        text-align: center;
      }
    }
  }
  // 二维码
  .cont {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 600rpx;
    height: 600rpx;
    padding: 20rpx;
    background-color: #fff;
    border-radius: 20rpx;
    text-align: center;
    .title {
      font-weight: 600;
      line-height: 60rpx;
    }
    .tips {
      font-size: $lxins-fs-12;
      color: #999;
      margin-bottom: 12rpx;
    }
    image {
      width: 460rpx;
      height: 460rpx;
      border: 2px solid #eee;
    }
  }
}
</style>
